package com.bestcem.xm.award.grpc.service.param.redpacket;

import com.bestcem.bp.grpc.v1.base.Page;
import com.bestcem.xm.award.grpc.service.param.draw.PageParam;
import com.bestcem.xm.award.grpc.v1.common.enums.DeliverChannelEnum;
import com.bestcem.xm.award.grpc.v1.enums.RedPacketStatusEnum;
import com.bestcem.xm.award.grpc.v1.services.ExportRedPacketWinRequest;
import com.bestcem.xm.award.grpc.v1.services.ListWinDetailsRequest;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author xa.zhang <xa.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/7/9 14:39
 * @desc 红包中奖明细列表查询参数
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class WinDetailListParam extends PageParam {

    /**
     * 红包状态 0-7，8全部状态(页面使用)
     */
    private Integer status;

    /**
     * 投放名称
     */
    private String deliverName;

    /**
     * 答卷编号
     */
    private Long seq;

    /**
     * 红包id
     */
    private String awardId;

    /**
     * 投放渠道 全部 0 ,二维码2，短信 3,微信公众号4,支付宝生活号5,邮件6
     */
    private Integer deliverChannel;

    private Integer start;

    public Integer getStart() {
        return (getPage() - 1) * getSize();
    }

    public void setStart(Integer start) {
        this.start = start;
    }

    /**
     * 列表导出 grpc参数转换
     *
     * @param request grpc参数
     * @return 转换后的参数
     */
    public static WinDetailListParam createFromExportRequest(ExportRedPacketWinRequest request) {

        return createRequest(request.getStatus(), request.getDeliverName(), request.getSeq(), request.getId(), request.getDeliverChannel());
    }

    /**
     * 列表查询 grpc参数转换
     *
     * @param request grpc参数
     * @return 转换后的参数
     */
    public static WinDetailListParam createFromRequest(ListWinDetailsRequest request) {
        WinDetailListParam param = createRequest(request.getStatus(), request.getDeliverName(), request.getSeq(), request.getId(), request.getDeliverChannel());

        Page page = request.getPage();
        if (page != null) {
            if (page.getSize() != 0) {
                param.setSize(page.getSize());
            }
            if (page.getPage() != 0) {
                param.setPage(page.getPage());
            }
        }

        return param;
    }

    /**
     * 列表查询公共参数部分
     *
     * @param status         红包状态
     * @param deliverName    投放名称
     * @param seq            答卷编号
     * @param id             红包id
     * @param deliverChannel 投放渠道
     * @return 转换后的参数
     */
    public static WinDetailListParam createRequest(RedPacketStatusEnum.RedPacketStatus status, String deliverName, int seq, String id, DeliverChannelEnum.DeliverChannel deliverChannel) {
        WinDetailListParam param = new WinDetailListParam();

        if (status.getNumber() != RedPacketStatusEnum.RedPacketStatus.ALL.getNumber()) {
            param.setStatus(status.getNumber());
        }

        param.setDeliverName(deliverName);
        if (seq != 0) {
            param.setSeq((long) seq);
        }
        param.setAwardId(id);

        if (deliverChannel.getNumber() != DeliverChannelEnum.DeliverChannel.ALL.getNumber()) {
            param.setDeliverChannel(deliverChannel.getNumber());
        }
        return param;
    }


}
